// 引入
const express = require('express');
const db = require('../../utils/db')

// 配置路由
const editRoter = express.Router();

// 编写接口
// 接收前端数据(渲染页面)
editRoter.all('/xinchou/get', (req, res) => {
    const page = req.query.page;
    const count = req.query.count;
    const sql = `SELECT * FROM company LIMIT ${(page - 1) * count},${count};`;
    const sql2=`SELECT COUNT(*) as total from company;`;
    db.query(sql, function (err, data) {
        if (err) {
            res.send({ error: 1, msg: err })
        } else {
            // res.send({ error: 0, msg: '添加成功', data: data });
            db.query(sql2, (err2, data2) => {
                if (err2) {
                    console.log(err2)
                    res.send({ error: 1, msg: err2 })
                } else {
                    console.log(data2)
                    res.send({ error: 0, msg: '查询成功', data: data, total: data2[0].total })
                }
            })
        }
    })
});
// 删除
editRoter.post('/xinchou/del', (req, res) => {
    const sql = 'delete from company where c_id="' + req.body.id + '";';
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '删除成功' })
        }
    })
})
// 查询
editRoter.post('/xinchou/inquire', (req, res) => {
    const sql = `SELECT * FROM company WHERE plan_id='${req.body.plan_id}' OR plan_name='${req.body.plan_name}' OR c_state='${req.body.c_state}';`
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: err })
        }else {
            res.send({ error: 0, msg: '查询成功' ,data: data})
        }
    })
})

// 新增
editRoter.all('/xinchou/add', (req, res) => {
    const sql =`INSERT INTO company VALUES(NULL,'${req.body.plan_id}','${req.body.plan_name}','${req.body.c_state}','${req.body.c_remark}','${req.body.c_crePerson}','${req.body.c_credate}','${req.body.c_update}');`
console.log(req.body);
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: err })
        }else {
            res.send({ error: 0, msg: '添加成功',data:data})
        }
    })
})

// 导出
module.exports = editRoter;

// 编辑
editRoter.post('/xinchou/edit', (req, res) => {
    const sql = `UPDATE company SET plan_name='${req.body.plan_name}',c_state='${req.body.c_state}',c_remark='${req.body.c_remark}',c_crePerson='${req.body.c_crePerson}',c_credate='${req.body.c_credate}',c_update='${req.body.c_update}' WHERE plan_id='${req.body.plan_id}';`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '修改成功' ,data:data})
        }
    })
});

// 新增下半截
editRoter.post('/xinchou/zengjia', (req, res) => {
    const sql = `SELECT * FROM salary_project;`;
    db.query(sql, function (err, data) {
        if (err) {
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '渲染成功', data: data });
            
        }
    })
});
// 模态框渲染
editRoter.post('/xinchou/modal', (req, res) => {
    const sql = `SELECT * FROM salary_project ;`;
    db.query(sql, function (err, data) {
        if (err) {
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '渲染成功', data: data });
            
        }
    })
});
// 模态框下拉
editRoter.post('/xinchou/xiala', (req, res) => {
    const sql = `SELECT * FROM salary_project;`;
    db.query(sql, function (err, data) {
        if (err) {
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '渲染成功', data: data })
            
        }
    })
});

editRoter.post('/xinchou/style', (req, res) => {
    const sql = `SELECT * FROM salary_project;`;
    db.query(sql, function (err, data) {
        if (err) {
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '渲染成功', data: data }) 
        }
    })
});
